import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QColor, QFont, QPainter, QBrush, QPen
from PyQt5.QtCore import QRect, Qt
from PyQt5.QtCore import QObject, pyqtSignal

"""自定义Switch按钮"""


class SwitchButton(QWidget):
    clicked = pyqtSignal(bool)

    def __init__(self, width, height, color='green', open_str='', close_str=''):
        super().__init__()
        self.color = color
        self.open_str = open_str
        self.close_str = close_str

        # 设置无边框和背景透明paintEvent
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setMinimumWidth(width)
        self.setMinimumHeight(height)
        self.setMaximumWidth(width)
        self.setMaximumHeight(height)
        self.state = False  # 按钮状态：True表示开，False表示关

    def mousePressEvent(self, event):
        """鼠标点击事件：用于切换按钮状态"""
        super(SwitchButton, self).mousePressEvent(event)

        self.state = False if self.state else True
        self.update()
        # 发射信号
        self.clicked.emit(self.state)

    # 重写paintEvent
    def paintEvent(self, event):
        """绘制按钮"""
        super(SwitchButton, self).paintEvent(event)

        # 创建绘制器并设置抗锯齿和图片流畅转换
        painter = QPainter(self)
        painter.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)

        # 定义字体样式
        font = QFont('Microsoft YaHei')
        font.setPixelSize(int(self.height()/2))
        painter.setFont(font)

        # 定义x偏移量
        x = int(self.height()/10)
        c = self.height()-(x*2)
        d = int(c/2)
        a = self.width()-c-x

        # 绘制背景
        painter.setPen(Qt.NoPen)

        # 开关为开的状态
        if self.state:
            brush = QBrush(QColor(self.color))
            painter.setBrush(brush)
            painter.drawRoundedRect(0, 0, self.width(), self.height(), int(self.height()/2), int(self.height()/2))

            # 绘制圆圈
            painter.setPen(Qt.NoPen)
            brush.setColor(QColor('#ffffff'))
            painter.setBrush(brush)
            painter.drawRoundedRect(a, x, c, c, d, d)

            # 绘制文本
            painter.setPen(QPen(QColor('#ffffff')))
            painter.setBrush(Qt.NoBrush)
            rect = QRect(2 * x, 0, self.height(), self.height())
            painter.drawText(rect, Qt.AlignVCenter | Qt.AlignHCenter, self.open_str)
        # 开关为关的状态
        else:
            brush = QBrush(QColor('#CCCCCC'))
            painter.setBrush(brush)
            painter.drawRoundedRect(0, 0, self.width(), self.height(), int(self.height()/2), int(self.height()/2))

            # 绘制圆圈
            brush = QBrush(QColor('#ffffff'))
            painter.setBrush(brush)
            painter.drawRoundedRect(x, x, c, c, d, d)

            # 绘制文本
            pen = QPen(QColor('#000000'))
            painter.setPen(pen)
            rect = QRect(self.width()-self.height()-x*2, 0, self.height(), self.height())
            painter.drawText(rect, Qt.AlignVCenter | Qt.AlignHCenter, self.close_str)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = SwitchButton(130, 60)
    window.show()
    sys.exit(app.exec())
